﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Services;
using Jn698._45Tool.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels.Controls.DataControls
{
    public class PortConfigViewModel:ViewModelBase
    {
        private GWCmdMakeUpService GWCmdMakeUpService = new GWCmdMakeUpService();
        private GWCmdMakeUpPlanService GWCmdMakeUpPlanService = new GWCmdMakeUpPlanService();
        private GWTempFrameService gWTempFrameService = new GWTempFrameService();
        public static GWCmdMakeUp GWCmdMakeUp;
        public static GWCmdMakeUpPlan GWCmdMakeUpPlan;
        public static string commControl;

        private string subName;

        public string SubName
        {
            get { return subName; }
            set { subName = value; RaisePropertyChanged(); }
        }

        private string oad;

        public string OAD
        {
            get { return oad; }
            set { if (Set(ref oad, value)) SaveData(); }
        }

        private IList<string> baudrateList;
        public IList<string> BaudrateList
        {
            get => baudrateList;
            set { if(Set(ref baudrateList, value)) SaveData(); }
        }

        private int baudrateIndex;

        public int BaudrateIndex
        {
            get { return baudrateIndex; }
            set { if(Set(ref baudrateIndex, value)) SaveData(); }
        }

        private IList<string> parityList;
        public IList<string> ParityList
        {
            get => parityList;
            set { if(Set(ref parityList, value)) SaveData(); }
        }

        private int parityIndex;

        public int ParityIndex
        {
            get { return parityIndex; }
            set { if(Set(ref parityIndex, value)) SaveData(); }
        }

        private IList<string> dataList;
        public IList<string> DataList
        {
            get => dataList;
            set { if(Set(ref dataList, value)) SaveData(); }
        }

        private int dataIndex;

        public int DataIndex
        {
            get { return dataIndex; }
            set { if(Set(ref dataIndex, value)) SaveData(); }
        }

        private IList<string> stopList;
        public IList<string> StopList
        {
            get => stopList;
            set { if(Set(ref stopList, value)) SaveData(); }
        }

        private int stopIndex;

        public int StopIndex
        {
            get { return stopIndex; }
            set { if(Set(ref stopIndex, value)) SaveData(); }
        }

        private IList<string> controlList;
        public IList<string> ControlList
        {
            get => controlList;
            set { if(Set(ref controlList, value)) SaveData(); }
        }

        private int funcIndex;

        public int FuncIndex
        {
            get { return funcIndex; }
            set { if(Set(ref funcIndex, value)) SaveData(); }
        }

        private IList<string> funclList;
        public IList<string> FunclList
        {
            get => funclList;
            set { if(Set(ref funclList, value)) SaveData(); }
        }

        private int controlIndex;

        public int ControlIndex
        {
            get { return controlIndex; }
            set { if(Set(ref controlIndex, value)) SaveData(); }
        }


        public PortConfigViewModel()
        {
            BaudrateList = new List<string>() { "300", "600", "1200", "2400", "4800", "7200", "9600", "19200", "38400", "57600", "115200" };
            ParityList = new List<string>() { "无校验", "奇校验", "偶校验" };
            DataList = new List<string>() { "5", "6", "7", "8" };
            StopList = new List<string>() { "1", "2" };
            ControlList = new List<string>() { "无", "硬件", "软件" };
            FunclList = new List<string>() { "上行通讯","抄表","级联","停用"};
            //GWCmdMakeUp = ServiceLocator.Current.GetInstance<GWCmdMakeUp>()??null;

        }
        public void UpdateData()
        {
            string[] datas;
            if (MainViewModel.TopIndex==0)
            {
                if (GWCmdMakeUp == null)
                {
                    return;
                }
                if (GWCmdMakeUp.SubItemName == null)
                {
                    return;
                }

                datas = GWCmdMakeUp.DataValue?.Split(';');
                SubName = GWCmdMakeUp.SubItemName;
            }
            else
            {
                if (GWCmdMakeUpPlan == null)
                {
                    return;
                }
                if (GWCmdMakeUpPlan.SubItemName == null)
                {
                    return;
                }

                datas = GWCmdMakeUpPlan.DataValue?.Split(';');
                SubName = GWCmdMakeUpPlan.SubItemName;
            }
            OAD = datas[0];
            BaudrateIndex = int.Parse(datas[1]);
            ParityIndex = int.Parse(datas[2]);
            DataIndex = int.Parse(datas[3]);
            StopIndex = int.Parse(datas[4]);
            ControlIndex = int.Parse(datas[5]);
            if (!SubName.Contains("红外"))
            {
                FuncIndex = int.Parse(datas[6]);
            }
            
        }

        private void SaveData()
        {
            string SubName = "";
            if (MainViewModel.TopIndex==0)
            {
                if (GWCmdMakeUp == null)
                {
                    return;
                }
                SubName = GWCmdMakeUp.SubItemName;
            }
            else
            {
                if (GWCmdMakeUpPlan == null)
                {
                    return;
                }
                SubName = GWCmdMakeUpPlan.SubItemName;
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(OAD);
            stringBuilder.Append(';');
            stringBuilder.Append(BaudrateIndex.ToString());
            stringBuilder.Append(';');
            stringBuilder.Append(ParityIndex.ToString());
            stringBuilder.Append(';');
            stringBuilder.Append(DataIndex.ToString());
            stringBuilder.Append(';');
            stringBuilder.Append(StopIndex.ToString());
            stringBuilder.Append(';');
            stringBuilder.Append(ControlIndex.ToString());
            stringBuilder.Append(';');
            if (!SubName.Contains("红外"))
            {
                stringBuilder.Append(FuncIndex.ToString());
                stringBuilder.Append(';');
            }
            if (MainViewModel.TopIndex==0)
            {
                GWCmdMakeUp.DataValue = stringBuilder.ToString();
                GWCmdMakeUpService.Update(GWCmdMakeUp);
            }
            else
            {
                GWCmdMakeUpPlan.DataValue = stringBuilder.ToString();
                GWCmdMakeUpPlanService.Update(GWCmdMakeUpPlan);
            }
        }
        public RelayCommand<Window> OKCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    commControl = string.Format("{0},{1},{2},{3},{4}", BaudrateIndex, ParityIndex, DataIndex, StopIndex, ControlIndex);
                    w.DialogResult = true;
                });
                return command;
            }
        }

        public RelayCommand<Window> CancelCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    w.DialogResult = false;
                });
                return command;
            }
        }
    }
}
